package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.Page;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.TbBrand;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbTypeTemplate;
import com.dongdong.service.BrandService;
import com.dongdong.service.SpecificationService;
import com.dongdong.service.TypeTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("templateController")
public class TemplateController {
    @Reference(timeout = 3000)
    private BrandService brandService;
    @Reference
    private SpecificationService specificationService;
    @Reference
    private TypeTemplateService typeTemplateService;
    @RequestMapping("toTemplate")
    public String toTemplate(Model model){
        List<TbBrand> brandList = brandService.selectAll();
        List<TbSpecification> specList = specificationService.selectAll();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specList",specList);
        return "admin/type_template";
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public Result selectAll(String name,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        Page<TbTypeTemplate> typeList = typeTemplateService.selectAll(name,pageNum,pageSize);
        try {
            return Result.respone(ResultEmnu.SUCCESS).setData(typeList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR).setData(typeList);
        }
    }
    @RequestMapping("insertInfo")
    @ResponseBody
    public Result insertInfo(TbTypeTemplate tbTypeTemplate){
          int i = typeTemplateService.insert(tbTypeTemplate);
          if (i > 0){
              return Result.respone(ResultEmnu.SUCCESS);
          }else {
              return Result.respone(ResultEmnu.ERROR);
          }
    }
    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id")Long id){
      try {
          TbTypeTemplate tbTypeTemplate = typeTemplateService.selectById(id);
          return Result.respone(ResultEmnu.SUCCESS).setData(tbTypeTemplate);
      }catch (Exception e){
          return Result.respone(ResultEmnu.ERROR);
      }
    }
   @RequestMapping("updateInfo")
   @ResponseBody
   public Result updateInfo(TbTypeTemplate tbTypeTemplate){
        try {
               int i = typeTemplateService.updateInfo(tbTypeTemplate);
               return Result.respone(ResultEmnu.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR);
        }
   }
    @RequestMapping("deleteAll")
    @ResponseBody
    public Result deleteAll(@RequestParam("ids[]")Long[] ids){
        int i = typeTemplateService.deleteAll(ids);
        if (i>0){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone((ResultEmnu.SUCCESS));
        }
    }
}
